package com.jza04.flightbooking.mapper;

import com.jza04.flightbooking.entity.Flight;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 航班Mapper接口
 */
@Mapper
public interface FlightMapper {
    
    /**
     * 查询所有航班
     */
    @Select("SELECT * FROM flight_JZA04")
    List<Flight> findAll();
    
    /**
     * 根据ID查询航班
     */
    @Select("SELECT * FROM flight_JZA04 WHERE flight_id = #{flightId}")
    Flight findById(String flightId);
    
    /**
     * 根据出发地和目的地查询航班
     */
    @Select("SELECT * FROM flight_JZA04 WHERE departure_city = #{departureCity} AND arrival_city = #{arrivalCity}")
    List<Flight> findByRoute(String departureCity, String arrivalCity);
    
    /**
     * 添加航班
     */
    @Insert("INSERT INTO flight_JZA04(flight_id, airline_company, departure_city, arrival_city, " +
            "departure_time, arrival_time, base_price, capacity, remaining_seats) " +
            "VALUES(#{flightId}, #{airlineCompany}, #{departureCity}, #{arrivalCity}, " +
            "#{departureTime}, #{arrivalTime}, #{basePrice}, #{capacity}, #{remainingSeats})")
    int insert(Flight flight);
    
    /**
     * 更新航班
     */
    @Update("UPDATE flight_JZA04 SET airline_company = #{airlineCompany}, departure_city = #{departureCity}, " +
            "arrival_city = #{arrivalCity}, departure_time = #{departureTime}, arrival_time = #{arrivalTime}, " +
            "base_price = #{basePrice}, capacity = #{capacity}, remaining_seats = #{remainingSeats} " +
            "WHERE flight_id = #{flightId}")
    int update(Flight flight);
    
    /**
     * 删除航班
     */
    @Delete("DELETE FROM flight_JZA04 WHERE flight_id = #{flightId}")
    int delete(String flightId);
    
    /**
     * 更新剩余座位数
     */
    @Update("UPDATE flight_JZA04 SET remaining_seats = remaining_seats - 1 WHERE flight_id = #{flightId} AND remaining_seats > 0")
    int decreaseRemainingSeats(String flightId);
    
    /**
     * 恢复座位
     */
    @Update("UPDATE flight_JZA04 SET remaining_seats = remaining_seats + 1 WHERE flight_id = #{flightId}")
    int increaseRemainingSeats(String flightId);
} 